/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.bean.CorpBean;
import com.koron.risk.domain.LocalCheckListEntry;
import com.koron.risk.service.api.LocalCheckListService;
import com.koron.risk.util.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "LocalCheckList", description = "本地检查表接口类")
@RestController
@RequestMapping("/LocalCheckList")
public class LocalCheckListController {
    @Autowired
    LocalCheckListService service;
    private final static Logger logger= LoggerFactory.getLogger(LocalCheckListController.class);

    @ApiOperation("草稿列表")
    @RequestMapping(value = "/getDraftList.htm", method = {RequestMethod.GET})
    public MessageBean<?> getDraftList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.getDraftList(user, corpCode);
    }

    @ApiOperation("获取详情")
    @ApiImplicitParam(name = "id", value = "检查表id" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/getDetails.htm", method = {RequestMethod.GET})
    public MessageBean<?> getDetails(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.details("local_checklist_entry", "local_checklist_item", id);
    }

    @ApiOperation("获取详情")
    @ApiImplicitParam(name = "code", value = "危险源code" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/detailsByCode.htm", method = {RequestMethod.GET})
    public MessageBean<?> detailsByCode(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.detailsByCode(code);
    }


    @ApiOperation("废弃检查表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "note", value = "说明", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approvalCode", value = "审核人", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/discard.htm", method = {RequestMethod.POST})
    @Log(title = "废弃本地检查",businessModule = "检查表管理", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> discard(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                  @RequestParam(value = "idList") List<String> idList, String title, String note, String approvalCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<?> msg = service.discard(idList, user, title, note, approvalCode);
        return msg;
    }

    @ApiOperation("提交操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "note", value = "说明", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approvalCode", value = "审核人", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/submitSourceAndRisk.htm", method = {RequestMethod.POST})
    @Log(title = "新增、修改本地检查表提交操作",businessModule = "检查表管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> submitSourceAndRisk(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                              @RequestBody List<LocalCheckListEntry> lceList, String title, String note, String approvalCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<?> msg = service.submit(lceList, user, title, note, "", approvalCode);
        return msg;
    }

    @ApiOperation("保存草稿")
    @RequestMapping(value = "/saveDraft.htm", method = {RequestMethod.POST})
    @Log(title = "新增、修改本地检查表保存草稿",businessModule = "检查表管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> saveDraft(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                    @RequestBody List<LocalCheckListEntry> lceList) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<?> msg = service.saveDraft(lceList, user);
        return msg;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "checklistEntryTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "Integer")})
    @ApiOperation("本地检查表列表")
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String corpCode, String businessTypeId, String checklistEntryTypeId, Integer state, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.list("local_checklist_entry", name, corpCode, businessTypeId, checklistEntryTypeId, state, page, pageSize);
    }


    @ApiImplicitParam(name = "id", value = "检查表id", required = true, paramType = "query", dataType = "String")
    @ApiOperation("导出")
    @RequestMapping(value = "/exportList.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            return null;
        }
        MessageBean<?> msg = service.details("local_checklist_entry", "local_checklist_item", id);
        if (msg.getData() == null) {
            return null;
        }
        File targetFile = new File("static/excel/LocalCheckEntry.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/LocalCheckEntry.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        LocalCheckListEntry data = (LocalCheckListEntry) msg.getData();
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        return Excel.createFile(data.getName(), targetFile, "LocalCheckEntry.btl", map);
    }

    @ApiOperation(value = "导入检查表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkCode", value = "检查表编号" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "checklistEntryTypeId", value = "检查表类型", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "note", value = "备注", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approvalCode", value = "审核人", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/importExcel.do", method = {RequestMethod.POST})
    public MessageBean<?> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                      String title, String note, String checkCode, String corpCode,
                                      String checklistEntryTypeId, String businessTypeId, MultipartFile uploadFile, String approvalCode) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.importExcel(user, checkCode, corpCode, checklistEntryTypeId, businessTypeId, title, note, uploadFile, approvalCode);
    }

    @ApiOperation("历史版本")
    @ApiImplicitParam(name = "code", value = "检查表code", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getHistoryList.htm", method = {RequestMethod.GET})
    public MessageBean<?> getHistoryList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String code) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.getHistoryList(code);
    }

    @ApiOperation("流程中的业务数据")
    @ApiImplicitParam(name = "ids", value = "流程关联的业务ids", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getProcessData.htm", method = {RequestMethod.GET})
    public MessageBean<?> getProcessData(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String ids) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.getProcessData(ids);
    }

    @ApiOperation(value = "下载检查表excel模板")
    @RequestMapping(value = "/downloadtemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> downloadTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/stdCheckEntryTemplate.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/stdCheckEntryTemplate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
        return Excel.createFile("检查表模板", targetFile, "default.btl", map);
    }

    @ApiOperation("删除草稿")
    @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String")
    @RequestMapping(value = "/deleteDraft.htm", method = {RequestMethod.GET})
    public MessageBean<?> deleteDraft(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<CorpBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        return service.deleteDraft(id);
    }


    @ApiOperation("检查表管理列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "危险源名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessTypeId", value = "业态" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "checklistEntryTypeId", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态" , paramType = "query", dataType = "Integer"),
    })
    @RequestMapping(value = "/exportCheckListEntryList.htm", method = RequestMethod.GET)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String corpCode,
                                String businessTypeId,String checklistEntryTypeId, Integer state) {
        if (null == user) {
            return null;
        }
        List<LocalCheckListEntry> list = service.exportList(user, corpCode, name,businessTypeId,checklistEntryTypeId,state);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/localCheckEntryList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/localCheckEntryList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("检查表管理列表", targetFile, "localCheckEntryList.btl", map);
    }
}
